package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.TimePickDialogUtil;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.AddressListAvtivity;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.AddressListBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;

/**
 * 进入法律援助申请表
 */
public class LifeServerApplyActivity extends BaseActivity implements View.OnClickListener,ContributorView {
    private ImageView regist_back;
    private TextView tv_headtitle;
    private ContributorPresenter contributorPresenter;
    private Context context;
    private String nameType;
    private String id;
    private LinearLayout layout_baojieserver;//保洁服务
    private RelativeLayout layout_yueshaoserver;//月嫂保姆
    private TextView tv_servertitlename;//设置上门时间还是面试时间
    private final String DefaultServiceAddrFlag="DefaultServiceAddrFlag";
    private final String ApplyServiceFlag="ApplyServiceFlag";
    private RelativeLayout layout_noaddress;
    private RelativeLayout layout_tianxieaddress;
    private boolean isaddress;
    private TextView tv_addressname;
    private TextView tv_addressphone;
    private TextView tv_address;
    String uid;
    private RelativeLayout layout_shangmentime;
    private TextView tv_shangmentime;
    private TextView tv_mianshitime;
    private Button bt_yuyue;
    private EditText tv_ayishuliang;
    private EditText tv_dashaotime;
    private EditText et_content;
    private MyToast myToast;
    private String addressId="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lifeserverapp_layout);
        context=this;
        myToast=new MyToast(context);
        contributorPresenter=new ContributorPresenter(this,context);
        Intent intent=getIntent();
        nameType=intent.getStringExtra("name");
        id=intent.getStringExtra("id");
        initView();
        if(nameType.equals("保洁服务"))
        {
            layout_baojieserver.setVisibility(View.VISIBLE);
        }else if(nameType.equals("月嫂保姆"))
        {
            layout_yueshaoserver.setVisibility(View.VISIBLE);
            tv_servertitlename.setText("面试时间");
        }else if(nameType.equals("水电维修"))
        {
            layout_yueshaoserver.setVisibility(View.VISIBLE);
            tv_servertitlename.setText("上门时间");
        }else if(nameType.equals("家电维修"))
        {
            layout_yueshaoserver.setVisibility(View.VISIBLE);
            tv_servertitlename.setText("上门时间");
        }
        uid=Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
    }

    private void initView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle = (TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("预约信息");
        tv_headtitle.requestFocus();
        tv_headtitle.setFocusable(true);
        tv_headtitle.setFocusableInTouchMode(true);
        layout_baojieserver = (LinearLayout) findViewById(R.id.layout_baojieserver);//保洁服务
        layout_yueshaoserver = (RelativeLayout) findViewById(R.id.layout_yueshaoserver);//月嫂保姆
        layout_yueshaoserver.setOnClickListener(this);
        tv_servertitlename = (TextView) findViewById(R.id.tv_servertitlename);//设置上门时间还是面试时间
        layout_noaddress = (RelativeLayout) findViewById(R.id.layout_noaddress);
        layout_noaddress.setOnClickListener(this);
        layout_tianxieaddress = (RelativeLayout) findViewById(R.id.layout_tianxieaddress);
        layout_tianxieaddress.setOnClickListener(this);
        tv_addressname = (TextView) findViewById(R.id.tv_addressname);
        tv_addressphone = (TextView) findViewById(R.id.tv_addressphone);
        tv_address = (TextView) findViewById(R.id.tv_address);
        layout_shangmentime = (RelativeLayout) findViewById(R.id.layout_shangmentime);
        layout_shangmentime.setOnClickListener(this);
        tv_shangmentime = (TextView) findViewById(R.id.tv_shangmentime);
        tv_mianshitime= (TextView) findViewById(R.id.tv_mianshitime);
        bt_yuyue=(Button) findViewById(R.id.bt_yuyue);
        bt_yuyue.setOnClickListener(this);
        tv_ayishuliang= (EditText) findViewById(R.id.tv_ayishuliang);
        tv_dashaotime= (EditText) findViewById(R.id.tv_dashaotime);
        et_content=(EditText) findViewById(R.id.et_content);
    }

    @Override
    protected void onResume() {
        super.onResume();
        contributorPresenter.getConvenienceDefaultServiceAddr(uid,DefaultServiceAddrFlag);
    }

    @Override
    public void onLoadContributorStart(String flag) {
        if (flag.equals(ApplyServiceFlag))
        {
            bt_yuyue.setEnabled(false);
        }

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if(flag.equals(DefaultServiceAddrFlag))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200"))
                {
                    String message=JsonTools.objectToJsonString(respObj.getMessage());
                    AddressListBean listData=JsonTools.jsonStringToObject(message,AddressListBean.class);
                    if(listData!=null &&listData.getType()>0)
                    {
                        isaddress=true;
                        tv_address.setText(listData.getAddressHead()+listData.getAddressFoot());
                        tv_addressname.setText(listData.getName());
                        addressId=listData.getId();
                        tv_addressphone.setText(listData.getPhone());
                    }
                }
            } else if (flag.equals(ApplyServiceFlag)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String messageString=(String) object;
                    if(!messageString.isEmpty())
                    {
                        if(messageString.equals("40"))
                        {
                            myToast.showShort("暂无商家,请稍后...");
                        }else if(messageString.equals("41"))
                        {
                            //预约成功，不需要支付
                            myToast.showShort("预约成功");
                            finish();
                        }else {
                            //预约成功，需要支付
                        }
                    }else {
                        myToast.showShort("预约失败");
                    }

                }else
                {
                    myToast.showShort("操作失败");
                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
        if(flag.equals(DefaultServiceAddrFlag))
        {
            if(isaddress)
            {
                layout_tianxieaddress.setVisibility(View.VISIBLE);
                layout_noaddress.setVisibility(View.GONE);
            }else
            {
                layout_tianxieaddress.setVisibility(View.GONE);
                layout_noaddress.setVisibility(View.VISIBLE);
                tv_address.setText("");
                tv_addressname.setText("");
                tv_addressphone.setText("");
            }

            isaddress=false;
        }else if (flag.equals(ApplyServiceFlag))
        {
            bt_yuyue.setEnabled(true);
        }
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.layout_noaddress:
                Intent intentaddress=new Intent(context,AddressListAvtivity.class);
                startActivity(intentaddress);
                break;
            case R.id.layout_tianxieaddress:
                Intent intentaddress1=new Intent(context,AddressListAvtivity.class);
                startActivity(intentaddress1);
                break;
            case R.id.layout_shangmentime:
                TimePickDialogUtil timePickDialogUtil = new TimePickDialogUtil(LifeServerApplyActivity.this);
                timePickDialogUtil.dateTimePicKDialog(tv_shangmentime);
                break;
            case R.id.layout_yueshaoserver:
                TimePickDialogUtil timePickDialogUtiltwo = new TimePickDialogUtil(LifeServerApplyActivity.this);
                timePickDialogUtiltwo.dateTimePicKDialog(tv_mianshitime);
                break;
            case R.id.bt_yuyue:
                if(nameType.equals("保洁服务"))
                {
                   postData(uid,tv_addressname.getText().toString().trim(),tv_addressphone.getText().toString().trim(),tv_address.getText().toString().trim(),
                           tv_ayishuliang.getText().toString().trim(),tv_dashaotime.getText().toString().trim(),tv_shangmentime.getText().toString().trim(),
                           et_content.getText().toString().trim()
                           );
                }else if(nameType.equals("月嫂保姆"))
                {
                    layout_yueshaoserver.setVisibility(View.VISIBLE);
                    tv_servertitlename.setText("面试时间");
                    postData(uid,tv_addressname.getText().toString().trim(),tv_addressphone.getText().toString().trim(),tv_address.getText().toString().trim(),
                            null,null,tv_mianshitime.getText().toString().trim(),
                            et_content.getText().toString().trim()
                    );

                }else if(nameType.equals("水电维修"))
                {
                    layout_yueshaoserver.setVisibility(View.VISIBLE);
                    tv_servertitlename.setText("上门时间");
                    postData(uid,tv_addressname.getText().toString().trim(),tv_addressphone.getText().toString().trim(),tv_address.getText().toString().trim(),
                            null,null,tv_mianshitime.getText().toString().trim(),
                            et_content.getText().toString().trim()
                    );
                }else if(nameType.equals("家电维修"))
                {
                    layout_yueshaoserver.setVisibility(View.VISIBLE);
                    tv_servertitlename.setText("上门时间");
                    postData(uid,tv_addressname.getText().toString().trim(),tv_addressphone.getText().toString().trim(),tv_address.getText().toString().trim(),
                            null,null,tv_mianshitime.getText().toString().trim(),
                            et_content.getText().toString().trim()
                    );
                }
                break;
        }
    }


    private void postData(String uid,String peoplename,String phone,String address,String ayinumber,String dashaohour,String shangmentime,String remark)
    {
        if(peoplename==null || peoplename.isEmpty())
        {
            myToast.showShort("预约地址中的名称不能为空");
            return;
        }
        if(phone==null || phone.isEmpty())
        {
            myToast.showShort("预约地址中的电话号码不能为空");
            return;
        }
        if(address==null || address.isEmpty())
        {
            myToast.showShort("预约地址不能为空");
            return;
        }
        if(nameType.equals("保洁服务"))
        {

            if(ayinumber==null||TextUtils.isEmpty(ayinumber))
            {
                myToast.showShort("阿姨数量不能为空");
                return;
            }
            if(dashaohour==null || dashaohour.isEmpty())
            {
                myToast.showShort("打扫时间不能为空");
                return;
            }
            if(shangmentime==null || shangmentime.isEmpty())
            {
                myToast.showShort("上门时间不能为空");
                return;
            }

        }else if(nameType.equals("月嫂保姆")) {
            if(shangmentime==null || shangmentime.isEmpty())
            {
                myToast.showShort("面试时间不能为空");
                return;
            }
        }else
        {
            if(shangmentime==null || shangmentime.isEmpty())
            {
                myToast.showShort("上门时间不能为空");
                return;
            }
        }
        try{
            if(dashaohour==null || ayinumber==null)
            {
                contributorPresenter.getConvenienceLifeserviceBook(uid,id,addressId,shangmentime,0,remark,0,ApplyServiceFlag);
            }else {
                contributorPresenter.getConvenienceLifeserviceBook(uid,id,addressId,shangmentime,Integer.parseInt(dashaohour),remark,Integer.parseInt(ayinumber),ApplyServiceFlag);
            }

        }catch (Exception e)
        {

        }

    }
}
